import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta:{title:'登录页'},
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '',
      redirect: '/vue3datascreen/faceScreen'
    },
    {
      path: '/vue3datascreen',
      name: 'vue3datascreen',
      component: () => import('../views/Layout.vue'),
      children: [
        {
          path: '/vue3datascreen',
          redirect: '/vue3datascreen/faceScreen'
        },
        {
          path: 'faceScreen',
          name: 'facescreen',
          meta:{title:'人脸实时监控大屏'},
          component: () => import('../views/screen/faceScreen.vue')
        },
        {
          path: 'rootfaceScreen',
          name: 'rootfaceScreen',
          meta:{title:'管理员版人脸实时监控大屏'},
          component: () => import('../views/screen/RootFaceScreen.vue')
        },
        {
    
          path: 'showfaceScreen',
          name: 'showfacescreen',
          meta:{title:'演示版人脸实时监控大屏'},
          component: () => import('../views/screen/showfaceScreen.vue')
      }
      ]
    },
  ]
})

// 前置路由守卫
router.beforeEach(async (to, from, next) => {
  if (to.path === '/login'|| to.path === '/vue3datascreen/showfaceScreen' || to.path === '/vue3datascreen/rootfaceScreen') {
    next()
  } else {
    const user = JSON.parse(sessionStorage.getItem('user'))
    if (user) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
